using System;
using UnityEngine;
using Yoozoo.Gta.Common;

namespace Yoozoo.Gameplay.Liberty.PlayerController
{
    public class PlayerInput : MonoBehaviour
    {
        [SerializeField]
        private bool closeMoveInput;
        public bool CloseMoveInput
        {
            get => closeMoveInput;
            set => closeMoveInput = value;
        }
        
        [SerializeField]
        private bool closeCameraInput;
        public bool CloseCameraInput
        {
            get => closeCameraInput;
            set => closeCameraInput = value;
        }
        
        public string horAxisName = "Horizontal";
        public string verAxisName = "Vertical";
        

        [SerializeField]
        private float _horValue;
        [SerializeField]
        private float _verValue;
        [SerializeField]
        private bool isSprint;
        [SerializeField] 
        private bool isDodge;
        
        public float HorRealValue
        {
            get
            {
                if (_horValue == 0)
                {
                    return horNavValue;
                }
                return _horValue;
            }
        }

        public float VerRealValue
        {
            get
            {
                if (_verValue == 0)
                {
                    return verNavValue;
                }
                return _verValue;
            }
        }

        public float horNavValue;
        public float verNavValue;
        
        public float HorValue => _horValue;
        public float VerValue => _verValue;
        public float MoveInputLength
        {
            get
            {
                return Mathf.Sqrt(HorValue * HorValue + VerValue * VerValue);
            }
        }

        public Vector2 MoveVector => new Vector2(HorValue, VerValue);

        [SerializeField]
        private bool _sprintValue;
        public bool SprintValue => _sprintValue;
        
        public bool IsSprint => isSprint;
        public bool IsDodge => isDodge;

        public string cameraHorAxisName = "Analog X";
        public string cameraVerAxisName = "Analog Y";

        [SerializeField] 
        private float _cameraHorValue;
        [SerializeField] 
        private float _cameraVerValue;

        public float CameraHorValue => _cameraHorValue;
        public float CameraVerValue => _cameraVerValue;

        public void SetSprint(bool sprint)
        {
            isSprint = sprint;
        }

        public void SetDodge(bool dodge)
        {
            isDodge = dodge;
        }

        private void Update()
        {

            _horValue = VirtualInput.GetAxis(horAxisName);
            _verValue = VirtualInput.GetAxis(verAxisName);
            
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
            if (Mathf.Abs(_horValue) <= 0.0001f && Mathf.Abs(_verValue) <= 0.0001f)
            {
                float x = Input.GetAxis("Horizontal");
                float y = Input.GetAxis("Vertical");
                // 椭圆映射
                _horValue = x * Mathf.Sqrt(1 - (y * y) / 2);
                _verValue = y * Mathf.Sqrt(1 - (x * x) / 2);
                
                bool lastSprintValue = _sprintValue;
                _sprintValue = Input.GetAxis("Sprint") > 0.1;
                if (lastSprintValue && !_sprintValue)
                {
                    // 抬起
                    ResidentHandler.Inst.GetFunction("CityLuaFunction.sprintBtnUp").Action();
                }
                else if (!lastSprintValue && _sprintValue)
                {
                    // 按下
                    ResidentHandler.Inst.GetFunction("CityLuaFunction.sprintBtnDown").Action();
                }
            }
#endif
            if (closeMoveInput)
            {
                _horValue = 0;
                _verValue = 0;
            }

            _cameraHorValue = VirtualInput.GetPanelAxis(cameraHorAxisName);
            _cameraVerValue = VirtualInput.GetPanelAxis(cameraVerAxisName);

// #if UNITY_EDITOR || UNITY_STANDALONE_WIN
//             if (Mathf.Abs(_cameraHorValue) <= 0.0001f && Mathf.Abs(_cameraVerValue) <= 0.0001f)
//             {
//                 if (Input.GetMouseButton(0))
//                 {
//                     _cameraHorValue = Input.GetAxis("Mouse X");
//                     _cameraVerValue = Input.GetAxis("Mouse Y");
//                 }
//             }
// #endif

            if (closeCameraInput)
            {
                _cameraHorValue = 0;
                _cameraVerValue = 0;
            }
        }
    }
}